package org.farm2.skc.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.farm2.base.db.FarmDbFields;
import org.farm2.base.domain.FarmUserContextLoader;
import org.farm2.base.exception.FarmAppException;
import org.farm2.base.exception.FarmExceptionUtils;
import org.farm2.base.password.FarmPasswordEncoder;
import org.farm2.skc.dao.SkcKnowFileDao;
import org.farm2.skc.domain.SkcKnowFile;
import org.farm2.skc.service.SkcKnowFileServiceInter;
import org.farm2.tools.bean.FarmBeanUtils;
import org.farm2.tools.db.DataQuery;
import org.farm2.tools.db.DataResult;
import org.farm2.tools.db.commons.DBRule;
import org.farm2.tools.db.commons.DBRuleList;
import org.farm2.tools.i18n.I18n;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**知识附件 
 * @author cbtg自动生成  2025-2-4 12:19:26 
 */
@Service
@Slf4j
public class SkcKnowFileServiceImpl implements SkcKnowFileServiceInter {


    @Autowired
    private SkcKnowFileDao skcKnowFileDao;

//    @Transactional
//    @Override
//    public SkcKnowFile insertSkcKnowFileEntity(SkcKnowFile skcKnowFile) {
//        FarmDbFields.initInsertBean(skcKnowFile, FarmUserContextLoader.getCurrentUser());
//        //FarmBeanUtils.runFunctionByBlank(skcKnowFile.getType(), "1", skcKnowFile::setType);
//        skcKnowFileDao.insert(skcKnowFile);
//       //[tree：树形结构使用]
//       //initTreeCode(actions.getId());
//        return skcKnowFile;
//    }
//
//    @Transactional
//    @Override
//    public SkcKnowFile editSkcKnowFileEntity(SkcKnowFile skcKnowFile) {
//        SkcKnowFile saveSkcKnowFile = getSkcKnowFileById(skcKnowFile.getId());
//        FarmExceptionUtils.throwNullEx(saveSkcKnowFile, I18n.msg("知识附件不存在:?", skcKnowFile.getId()));
//        saveSkcKnowFile.setId(skcKnowFile.getId());
//        saveSkcKnowFile.setFileid(skcKnowFile.getFileid());
//        saveSkcKnowFile.setKnowid(skcKnowFile.getKnowid());
//        saveSkcKnowFile.setVersionid(skcKnowFile.getVersionid());
//        saveSkcKnowFile.setType(skcKnowFile.getType());
//
//        FarmDbFields.initUpdateBean(saveSkcKnowFile, FarmUserContextLoader.getCurrentUser());
//        skcKnowFileDao.update(saveSkcKnowFile);
//        return saveSkcKnowFile;
//    }

    @Transactional
    @Override
    public SkcKnowFile getSkcKnowFileById(String id) {
        return skcKnowFileDao.findById(id);
    }

    @Override
    public List<SkcKnowFile> getSkcKnowFiles(DataQuery query) {
        return skcKnowFileDao.queryData(query.setCount(false)).getData(SkcKnowFile.class);
    }



    @Transactional
    @Override
    public DataResult searchSkcKnowFile(DataQuery query) {
        DataResult result = skcKnowFileDao.queryData(query);
        return result;
    }

    @Override
    public int getSkcKnowFileNum(DataQuery query) {
        return skcKnowFileDao.countData(query);
    }


    @Transactional
    @Override
    public void delSkcKnowFile(String id) {
        /*[tree：树形结构使用]
        if ( skcKnowFileDao.findByParentId(id).size() > 0) {
            throw new RuntimeException("不能删除该节点，请先删除其子节点");
        }
        */
        skcKnowFileDao.deleteById(id);
    }
    
    @Override
    public int getNum(DataQuery query) {
        return  skcKnowFileDao.countData(query);
    }

    @Override
    public int getNumByKnowId(String knowid) {
        return skcKnowFileDao.countData(DBRuleList.getInstance().add(new DBRule("KNOWID", knowid, "=")).toList());
    }
    /*[tree：树形结构使用]
    @Transactional
    @Override
    public void moveTreeNode(List<String> sourceIds, String targetId) {
        for (String sourceId : sourceIds) {
            // 移动节点
            SkcKnowFile node = getSkcKnowFileById(sourceId);
            if (!"NONE".equals(targetId)) {
                SkcKnowFile target = getSkcKnowFileById(targetId);
                if (target.getTreecode().indexOf(node.getTreecode()) >= 0) {
                    throw new RuntimeException("不能够移动到其子节点下!");
                }
            }
            node.setParentid(targetId);
            skcKnowFileDao.update(node);
            // 构造所有树TREECODE
            List<SkcKnowFile> list = skcKnowFileDao.findSubNodes(sourceId);
            for (SkcKnowFile treenode : list) {
                initTreeCode(treenode.getId());
            }
        }
    }*/
    
     /**[tree：树形结构使用]
      * 构造treecode字段
     * @param treeNodeId
    private void initTreeCode(String treeNodeId) {
        SkcKnowFile node = skcKnowFileDao.findById(treeNodeId);
        if (node.getParentid().equals("NONE")) {
            node.setTreecode(node.getId());
        } else {
            node.setTreecode(skcKnowFileDao.findById(node.getParentid()).getTreecode() + node.getId());
        }
        skcKnowFileDao.update(node);
    }
     */
    /* [tree：树形结构使用]
    @Transactional
    @Override
    public void autoSort(List<String> ids) {
        int sort = 0;
        for (String id : ids) {
            SkcKnowFile node =  skcKnowFileDao.findById(id);
            if (sort == 0) {
                sort = node.getSortcode();
            }
            node.setSortcode(sort++);
            skcKnowFileDao.update(node);
        }
    }*/
}
